package com.shenma2009;

import java.util.HashMap;

/**
 * @author 军哥
 * @version 1.0
 * @description: TestCopyApp
 * @date 2023/5/23 11:20
 */

class Technical implements Cloneable{
    private String tech;

    public String getTech() {
        return tech;
    }

    public void setTech(String tech) {
        this.tech = tech;
    }

    @Override
    public String toString() {
        return tech;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

class Person implements Cloneable{
    private String name;
    private Integer age;
    private Technical technical;

    public String getName() {
        return name;
    }

    public Integer getAge() {
        return age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Technical getTechnical() {
        return technical;
    }

    public void setTechnical(Technical technical) {
        this.technical = technical;
    }

    @Override
    public String toString() {
        return this.name+this.age+ this.technical.toString();
    }

    /**
     * @description 对克隆函数进行了深拷贝处理
     * @author 军哥
     * @date 2023/5/23 11:56
     * @version 1.0
     */

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Person t = (Person)super.clone();
        Object clone = technical.clone();
        t.setTechnical((Technical)clone);
        return t;
    }
}

public class TestCopyApp {
    public static void main(String[] args) throws CloneNotSupportedException {

        Technical technical = new Technical();
        technical.setTech("善于开车");

        Person person1 = new Person();
        person1.setName("jiagnziheng");
        person1.setAge(66);
        person1.setTechnical(technical);

        Person person2 = (Person)person1.clone();
        person2.setAge(16);
        person2.getTechnical().setTech("善于游泳");


        System.out.println(person1.toString());
        System.out.println(person2.toString());

    }
}
